{{- define "title" }}{{ .Title }} - {{ .Site.Title }}{{ end -}}

{{- define "content" -}}
    {{- $params := .Scratch.Get "params" -}}

    {{- $toc := $params.toc -}}
    {{- if eq $toc true -}}
        {{- $toc = .Site.Params.page.toc | default dict -}}
    {{- else if eq $toc false -}}
        {{- $toc = dict "enable" false -}}
    {{- end -}}

    {{- /* Auto TOC */ -}}
    {{- if ne $toc.enable false -}}
        <div class="toc" id="toc-auto">
            <h2 class="toc-title">{{ T "contents" }}</h2>
            <div class="toc-content{{ if eq $toc.auto false }} always-active{{ end }}" id="toc-content-auto"></div>
        </div>
    {{- end -}}

    <article class="page single" data-toc="{{ if ne $toc.enable true }}disable{{ else }}enable{{ end }}">

        {{- /* Featured image */ -}}
        {{- $image := $params.featuredimage -}}
        {{- with .Resources.GetMatch "featured-image" -}}
            {{- $image = .RelPermalink -}}
        {{- end -}}
        {{- with $image -}}
            <div class="featured-image">
                {{- dict "Src" . "Title" $.Description "Resources" $.Resources | partial "plugin/image.html" -}}
            </div>
        {{- end -}}

        <div class="single-card" {{ with $image }}data-image="true"{{ end }}>
            {{- /* Title */ -}}
            <h2 class="single-title animated flipInX">{{ .Title }}</h2>

            {{- /* Subtitle */ -}}
            {{- with $params.subtitle -}}
                <h2 class="single-subtitle">{{ . }}</h2>
            {{- end -}}

            {{- /* Meta */ -}}
            <div class="post-meta">
                <div class="post-meta-line">
                    {{- $author := $params.author | default .Site.Author.name | default (T "author") -}}
                    {{- $authorLink := $params.authorlink | default .Site.Author.link | default .Site.Home.RelPermalink -}}
                    <span class="post-author">
                        {{- $options := dict "Class" "author" "Destination" $authorLink "Title" "Author" "Rel" "author" "Icon" (dict "Class" "fas fa-user-circle fa-fw") "Content" $author -}}
                        {{- partial "plugin/link.html" $options -}}
                    </span>

                    {{- $categories := slice -}}
                    {{- range .Params.categories -}}
                        {{- $category := partialCached "function/path.html" . . | printf "/categories/%v" | $.Site.GetPage -}}
                        {{- $categories = $categories | append (printf `<a href="%v"><i class="far fa-folder fa-fw"></i>%v</a>` $category.RelPermalink $category.Title) -}}
                    {{- end -}}
                    {{- with delimit $categories "&nbsp;" -}}
                        &nbsp;<span class="post-category">
                            {{- dict "Categories" . | T "publishedInCategories" | safeHTML -}}
                        </span>
                    {{- end -}}
                </div>
                <div class="post-meta-line">
                    {{- with .Site.Params.dateformat | default "2006-01-02" | .PublishDate.Format -}}
                    <span><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="{{ . }}">{{ . }}</time></span>&nbsp;
                    {{- end -}}
                    {{- with $params.license | string -}}
                    <span><i class="fas fa-balance-scale fa-fw"></i>&nbsp;{{- . | safeHTML -}}</span>&nbsp;
                    {{- end -}}
                    <span><i class="fas fa-pencil-alt fa-fw"></i>&nbsp;{{ T "wordCount" .WordCount }}</span>&nbsp;
                    <span><i class="far fa-clock fa-fw"></i>&nbsp;{{ T "readingTime" .ReadingTime }}</span>&nbsp;
                    {{- $comment := .Scratch.Get "comment" | default dict -}}
                    {{- if $comment.enable | and $comment.valine.enable | and $comment.valine.visitor -}}
                        <span id="{{ .RelPermalink }}" class="leancloud_visitors" data-flag-title="{{ .Title }}">
                            <i class="far fa-eye fa-fw"></i>&nbsp;<span class=leancloud-visitors-count></span>&nbsp;{{ T "views" }}
                        </span>&nbsp;
                    {{- end -}}
                </div>
            </div>
            
            <hr>

            {{- /* Static TOC */ -}}
            {{- if ne $toc.enable false -}}
                <div class="details toc" id="toc-static"  data-kept="{{ if $toc.keepStatic }}true{{ end }}">
                    <div class="details-summary toc-title">
                        <span>{{ T "contents" }}</span>
                        <span><i class="details-icon fas fa-angle-right"></i></span>
                    </div>
                    <div class="details-content toc-content" id="toc-content-static">
                        {{- dict "Content" .TableOfContents "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
                    </div>
                </div>
            {{- end -}}

            {{- /* Content */ -}}
            <div class="content" id="content">
                {{- $content := .Content -}}
                {{- $content = dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" -}}
                {{- $content | replaceRE "<p><figure>" "<figure>" | replaceRE "(?s:</figure>\n?</p>)" "</figure>" | replaceRE "https://www.w3.org/" "http://www.w3.org/"| replaceRE "font-weight:550" "font-weight:500" | replaceRE "<section" "<div" | replaceRE "</section" "</div" | replaceRE `<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">` `<div class="video">` | replaceRE ` style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;"` `` | replaceRE " webkitallowfullscreen mozallowfullscreen" "" | replaceRE "allowfullscreen" `allow="fullscreen"` | replaceRE `type="application/javascript"` `` | replaceRE `charset="utf-8"></script>` `></script>` | replaceRE `<div id="fb-root"></div>` `` | safeHTML -}}
            </div>

            {{- /* Footer */ -}}
            {{- partial "single/footer.html" . -}}
        </div>

        {{- /* Comment */ -}}
        {{- partial "comment.html" . -}}
    </article>
{{- end -}}
